//
//  MWLocationAutoView.m
//  FreeTime
//
//  Created by 王江华 on 16/8/26.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//

#import "MWLocationAutoView.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Header.h"


@interface MWLocationAutoView()

@property(nonatomic,strong)MKMapView *mapView;
@property(nonatomic,strong)UIImageView *topImageView;
@property(nonatomic,strong)UIView       *bgView;
@property(nonatomic,strong)UIImageView *headImageView;
@property(nonatomic,strong)UILabel    *notLabel;

@property(nonatomic,assign)CGFloat TOP;
@property(nonatomic,assign)CGFloat LEFT;
@property(nonatomic,assign)CGFloat HEADWIDTH;

@end

@implementation MWLocationAutoView

@synthesize TOP;
@synthesize LEFT;
@synthesize HEADWIDTH;

+(MWLocationAutoView *)shardAutoView
{
    static MWLocationAutoView *locationAuto = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        locationAuto = [[MWLocationAutoView alloc] init];
    });
    return locationAuto;
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        
        if (WindowHeight <= 480) {
            TOP = 70;
            LEFT = 50;
            HEADWIDTH = 60.0f;
            
        }else{
            TOP = 100;
            LEFT = 30;
            HEADWIDTH = 60.0f;
        }
    }
    return self;
}

-(void)showOnView:(UIView *)view withText:(NSString *)text
{
    [self dismiss];
    MKCoordinateSpan span;
   // NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:MW_LAT];
  //  NSString *log = [[NSUserDefaults standardUserDefaults] objectForKey:MW_LOG];
   // DLog(@"lat = %@, log = %@",lat,log);
    //CLLocationCoordinate2D core2D = CLLocationCoordinate2DMake([lat floatValue],[log floatValue]);
    MKCoordinateRegion region;
    span.latitudeDelta=5 / 180.0f;
    span.longitudeDelta= 5 / 360.0f;
    region.span=span;
    //region.center=core2D;
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
    
    [self addSubview:self.bgView];
    [self addSubview:self.mapView];
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:HEADIMG1];
    if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
        UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
        _headImageView.image = image;
    }else{
        NSString *image = [[NSUserDefaults standardUserDefaults] objectForKey:MW_HEADIMG];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:image]];
    }
    self.notLabel.text = text;
    [self addSubview:self.notLabel];
    [self addSubview:self.topImageView];
    self.headImageView.center = self.topImageView.center;
    [self addSubview:self.headImageView];
    [self rotate360DegreeWithImageView:self.topImageView];
    [self.topImageView startAnimating];
    [view addSubview:self];
}
-(void)dismiss
{
    [self.layer removeAllAnimations];
    [self.bgView removeFromSuperview];
    [self.mapView removeFromSuperview];
    [self.notLabel removeFromSuperview];
    [self.topImageView removeFromSuperview];
    [self.headImageView removeFromSuperview];
    [self removeFromSuperview];
}

-(UIImageView *)headImageView
{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, HEADWIDTH, HEADWIDTH)];
        _headImageView.layer.cornerRadius = HEADWIDTH / 2;
        _headImageView.clipsToBounds = YES;
        _headImageView.layer.borderColor = [UIColor whiteColor].CGColor;
        _headImageView.layer.borderWidth = 2;
        
    }
    return _headImageView;
}

-(UILabel *)notLabel
{
    if (!_notLabel) {
        _notLabel = [[UILabel alloc] initWithFrame:CGRectMake(LEFT , TOP + WindowWidth - 2*LEFT + 20, WindowWidth - 2 * LEFT, 20)];
        _notLabel.textColor =TEXTCOLOR ;
        _notLabel.textAlignment = NSTextAlignmentCenter;
        _notLabel.font = [UIFont systemFontOfSize:13.0f];
        _notLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _notLabel;
}
-(MKMapView *)mapView
{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(LEFT + 1, TOP + 1, WindowWidth - 2 * LEFT - 2, WindowWidth - 2 * LEFT - 2)];
        _mapView.userInteractionEnabled = NO;
        _mapView.layer.cornerRadius = _mapView.frame.size.width / 2;
        _mapView.clipsToBounds = YES;
        _mapView.mapType = MKMapTypeStandard;
        
    }
    return _mapView;
}

-(UIImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(LEFT, TOP, WindowWidth - 2*LEFT, WindowWidth - 2*LEFT)];
        _topImageView.image = [UIImage imageNamed:@"M_loading"];
    }
    return _topImageView;
}

-(UIImageView *)rotate360DegreeWithImageView:(UIImageView *)imageView{
    CABasicAnimation *animation = [ CABasicAnimation
                                   animationWithKeyPath: @"transform" ];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    
    //围绕Z轴旋转，垂直与屏幕
    animation.toValue = [ NSValue valueWithCATransform3D:
                         
                         CATransform3DMakeRotation(-M_PI / 2, 0.0, 0.0, -1.0) ];
    animation.duration = 2.0;
    //旋转效果累计，先转180度，接着再旋转180度，从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = 1000;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [imageView.layer addAnimation:animation forKey:nil];
    return imageView;
}

-(UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = [UIColor clearColor];
    }
    return _bgView;
}




@end
